/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.drive.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.common.constant.FlowDicConstant;
import com.hopes.bizz.basic.drive.dto.BaseAuditDTO;
import com.hopes.bizz.basic.drive.dto.BaseDriveStopFlowSearchDTO;
import com.hopes.bizz.basic.drive.dto.BaseDriveStopRecordDTO;
import com.hopes.bizz.basic.drive.dto.BaseDriveStopRecordExcelDTO;
import com.hopes.bizz.basic.drive.entity.BaseDriveStopRecordEntity;
import com.hopes.bizz.basic.drive.service.BaseDriveStopRecordService;
import com.hopes.bizz.basic.drive.vo.BaseDriveStopRecordExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 开停车申报
 *
 * @author pig
 * @date 2023-11-02 09:27:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/stopRecord")
@Tag(description = "stopRecord", name = "开停车申报管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BaseDriveStopRecordController {

    private final BaseDriveStopRecordService baseDriveStopRecordService;

    /**
     * 分页查询
     *
     * @param page                分页对象
     * @param baseDriveStopRecord 开停车申报
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_view')")
    public R getbaseDriveStopRecordPage(Page page, BaseDriveStopRecordDTO baseDriveStopRecord) {
        LambdaQueryWrapper<BaseDriveStopRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseDriveStopRecord.getOrgId()), BaseDriveStopRecordEntity::getOrgId, baseDriveStopRecord.getOrgId());
        wrapper.like(StrUtil.isNotBlank(baseDriveStopRecord.getFacilityName()), BaseDriveStopRecordEntity::getFacilityName, baseDriveStopRecord.getFacilityName());
        wrapper.eq(StrUtil.isNotBlank(baseDriveStopRecord.getRecordStatus()), BaseDriveStopRecordEntity::getRecordStatus, baseDriveStopRecord.getRecordStatus());
        wrapper.ge(ObjectUtil.isNotNull(baseDriveStopRecord.getStartDate()), BaseDriveStopRecordEntity::getStartDate, baseDriveStopRecord.getStartDate());
        wrapper.le(ObjectUtil.isNotNull(baseDriveStopRecord.getStopDate()), BaseDriveStopRecordEntity::getStopDate, baseDriveStopRecord.getStopDate());
        wrapper.orderByDesc(BaseDriveStopRecordEntity::getOrgId);
        return R.ok(baseDriveStopRecordService.page(page, wrapper));
    }


    /**
     * 申报界面分页查询
     *
     * @param page                分页对象
     * @param baseDriveStopRecord 开停车申报
     * @return
     */
    @Operation(summary = "申报界面分页查询", description = "申报界面分页查询")
    @GetMapping("/declarePage")
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_view')")
    public R getbaseDriveStopDeclarePage(Page page, BaseDriveStopRecordDTO baseDriveStopRecord) {
        LambdaQueryWrapper<BaseDriveStopRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseDriveStopRecord.getOrgId()), BaseDriveStopRecordEntity::getOrgId, baseDriveStopRecord.getOrgId());
        wrapper.like(StrUtil.isNotBlank(baseDriveStopRecord.getFacilityName()), BaseDriveStopRecordEntity::getFacilityName, baseDriveStopRecord.getFacilityName());
        wrapper.ge(ObjectUtil.isNotNull(baseDriveStopRecord.getStartDate()), BaseDriveStopRecordEntity::getStartDate, baseDriveStopRecord.getStartDate());
        wrapper.le(ObjectUtil.isNotNull(baseDriveStopRecord.getStopDate()), BaseDriveStopRecordEntity::getStopDate, baseDriveStopRecord.getStopDate());
        wrapper.and(item->item.eq(BaseDriveStopRecordEntity::getRecordStatus, FlowDicConstant.NOT_PASS).or().eq(BaseDriveStopRecordEntity::getRecordStatus, FlowDicConstant.FILL_IN));
        wrapper.orderByDesc(BaseDriveStopRecordEntity::getOrgId);
        return R.ok(baseDriveStopRecordService.page(page, wrapper));
    }



    /**
     * 通过id查询开停车申报
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(baseDriveStopRecordService.getById(id));
    }

    /**
     * 新增开停车申报
     *
     * @param baseDriveStopRecord 开停车申报
     * @return R
     */
    @Operation(summary = "新增开停车申报", description = "新增开停车申报")
    @SysLog("新增开停车申报")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_add')")
    public R save(@RequestBody BaseDriveStopRecordEntity baseDriveStopRecord) {
        // 填报中
        baseDriveStopRecord.setRecordStatus(FlowDicConstant.FILL_IN);
        return R.ok(baseDriveStopRecordService.save(baseDriveStopRecord));
    }

    /**
     * 修改开停车申报
     *
     * @param baseDriveStopRecord 开停车申报
     * @return R
     */
    @Operation(summary = "修改开停车申报", description = "修改开停车申报")
    @SysLog("修改开停车申报")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_edit')")
    public R updateById(@RequestBody BaseDriveStopRecordEntity baseDriveStopRecord) {
        return R.ok(baseDriveStopRecordService.updateById(baseDriveStopRecord));
    }

    /**
     * 通过id删除开停车申报
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除开停车申报", description = "通过id删除开停车申报")
    @SysLog("通过id删除开停车申报")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_del')")
    public R removeById(@RequestBody Long[] ids) {
        baseDriveStopRecordService.deleteTask(CollUtil.toList(ids));
        return R.ok();
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_export')")
    public List<BaseDriveStopRecordExcelVO> template() {
        return baseDriveStopRecordService.exportExcel(Collections.singletonList(new BaseDriveStopRecordEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param baseDriveStopRecord 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_export')")
    public List<BaseDriveStopRecordExcelVO> export(BaseDriveStopRecordEntity baseDriveStopRecord) {
        LambdaQueryWrapper<BaseDriveStopRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(baseDriveStopRecord.getOrgId()), BaseDriveStopRecordEntity::getOrgId, baseDriveStopRecord.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(baseDriveStopRecord.getFacilityName()), BaseDriveStopRecordEntity::getFacilityName, baseDriveStopRecord.getFacilityName());
        wrapper.eq(StrUtil.isNotBlank(baseDriveStopRecord.getRecordStatus()), BaseDriveStopRecordEntity::getRecordStatus, baseDriveStopRecord.getRecordStatus());
        wrapper.ge(ObjectUtil.isNotNull(baseDriveStopRecord.getStartDate()), BaseDriveStopRecordEntity::getStartDate, baseDriveStopRecord.getStartDate());
        wrapper.le(ObjectUtil.isNotNull(baseDriveStopRecord.getStopDate()), BaseDriveStopRecordEntity::getStopDate, baseDriveStopRecord.getStopDate());
        return baseDriveStopRecordService.exportExcel(baseDriveStopRecordService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param baseDriveStopRecordExcelDtoList 查询条件
     * @param bindingResult                   错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('drive_stopRecord_export')")
    public R importExcel(@RequestExcel List<BaseDriveStopRecordExcelDTO> baseDriveStopRecordExcelDtoList, BindingResult bindingResult) {
        return baseDriveStopRecordService.importExcel(baseDriveStopRecordExcelDtoList, bindingResult);
    }

    @Operation(summary = "查询我的待审批", description = "查询我的待审批")
    @GetMapping("/queryTodoForPage")
    public R queryTodoForPage(Page page, BaseDriveStopFlowSearchDTO flowSearch) {
        flowSearch.setTaskKey(FlowDicConstant.IN_REVIEW);
        Page res = baseDriveStopRecordService.queryTodoForPage(page, flowSearch);
        return R.ok(res);
    }

    @Operation(summary = "提交开停车审批结果", description = "提交开停车审批结果")
    @SysLog("提交开停车审批结果")
    @PostMapping("/handlerAudit")
    public R handlerAudit(@Validated @RequestBody BaseAuditDTO baseAuditDTO) {
        baseDriveStopRecordService.handlerAudit(baseAuditDTO);
        return R.ok();
    }

    @Operation(summary = "提交", description = "提交")
    @SysLog("提交")
    @GetMapping("/driveSubmit/{id}")
    public R driveSubmit(@PathVariable("id")  Long id) {
        baseDriveStopRecordService.handleDriveSubmit(id);
        return R.ok();
    }
}
